using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sfcms.api;
using System.Text;

public partial class sfcms_sfcms_placeholder : System.Web.UI.UserControl, iplaceholderuicontrol
{
    public int Width, Height;

    public void DisplayPlaceHolder()
    {
        if (page.current == null) return;
        
        bool isPreview = Request.QueryString["sfcmspreview"] == "1";
        
        contentdiv.Attributes.Add("style", "width:" + Width + "px;height:" + Height + "px;");

        if (sfcms.api.user.current != null && !isPreview) contentdiv.Attributes["style"] += "border:2px dashed #c0c0c0;";

        if (sfcms.api.user.current != null && page.current.id > 0 && !isPreview)
        {
            contentdiv.Attributes.Add
            (
                "ondblclick",
                string.Format
                (
                    "document.getElementById('{5}').value = document.getElementById('{4}').innerHTML = window.showModalDialog('/web/sfcms/sfcms_editplaceholdercontents.aspx?pageID={0}&controlID={1}&w={2}&h={3}', document.getElementById('{4}').innerHTML, 'dialogWidth=800px;dialogHeight:600px;');",
                    page.current.id,
                    ID,
                    Width,
                    Height,
                    contentdiv.ClientID,
                    htmlContents.ClientID
                )
            );
        }

        if (sfcms.api.user.current != null && page.current.id == 0 && !isPreview)
        {
            contentdiv.Attributes.Add("ondblclick", "javascript:alert('Please save the page to edit the contents of this placeholder');");
        }

        if (!isPreview)
        {
            foreach (placeholder p in page.current.placeholders)
            {
                if (p.controlid == this.ID)
                {
                    contentdiv.InnerHtml = Server.HtmlDecode(p.content);
                    if (sfcms.api.user.current != null) htmlContents.Value = Server.HtmlDecode(p.content);
                }
            }
        }
        else
        {
            contentdiv.InnerHtml = htmlContents.Value;
        }
    }

    public void SavePlaceHolder()
    {
        foreach (placeholder p in page.current.placeholders)
        {
            if (p.controlid == this.ID)
            {
                p.content = htmlContents.Value;
                p.save();
                return;
            }
        }

        placeholder newp = new placeholder();
        newp.content = htmlContents.Value;
        newp.controlid = ID;
        newp.pageid = page.current.id;
        newp.save();
    }
}
